<?php
defined('BY_JYA') or exit('error');
$return=array();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['account']=$_SESSION['account'];
$return['extradata']=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$onepluginname=$this->model('goods')->is_onegoods($_SESSION['current_plugin'],$_RQ['pluginname']);
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
	}else{
			$params['plugin_name OR']='plugin_name=\'jy_weishop\' or plugin_name=\'\' or plugin_name=\''.$_SESSION['current_plugin'].'\'';
	}
	$fenlei=pdo_getall('core_account_article_fenlei',$params,array('id','title','picurl'));
	$return['url']=custom_url('jy_weishop/welcome.home/',true);	
	if($fenlei){
		foreach($fenlei as &$item){
			$item['url']=custom_url('jy_weishop/news.list//group_id='.$item['id'],true);
			$item['picurl'] = $item['picurl'] ? $item['picurl'] : '../core/resource/images/nopic.jpg';
		}
	}
	$return['item']['fenleiurl']=custom_url('jy_weishop/news.list//fenlei='.$return['item']['fenlei'],true);
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$fenleiid=intval($_RQ['group_id']);
	if($fenleiid){
		$params['fenlei']=$fenleiid;
	}
	if($onepluginname){//开启独立商品
			$params['plugin_name']=$onepluginname;
			$return['params']['plugin_name']=$onepluginname;
	}else{
			$params['plugin_name OR']='plugin_name=\'jy_weishop\' or plugin_name=\'\' or plugin_name=\''.$_SESSION['current_plugin'].'\'';
	}
	$fields=array('id','title','content','fenlei','keyword','createtime','readnum','likenum','status','desc','pic_url','yingxiao');
	$orderby=array();
	$_RQ['page'] = $_RQ['page'] ? $_RQ['page'] : 1;
	$pindex = max(1, intval($_RQ['page']));
	if(intval($_RQ['num'])){
		$psize = intval($_RQ['num']);
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,36);
			$item['picurl'] = $item['pic_url'] ? $item['pic_url'] : '../core/resource/images/nopic.jpg';
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['fenlei']=$fenlei[$item['fenlei']]['title'];
			$item['edit']=array('op'=>'edit','action'=>'news.list','query'=>'id='.$item['id']);
			$item['url']=custom_url('jy_weishop/news.list/detail/id='.$item['id'],true);
		}
	}
	$group_id = intval($_RQ['group_id']);
	$sharelink='jy_weishop/news.list/list/fromuser='.$_SESSION['uid'].'&group_id='.$group_id;
	$return['sharedata']=array(
			'title'=>$_RQ['group_id'] ? $return['fenlei'][$group_id]['title'] . '-' . $_SESSION['account']['title']: '文章分类 -'.$_SESSION['account']['title'],
			'desc'=>$_SESSION['account']['title'].'-文章列表',
			'link'=>custom_url($sharelink,true)
	);
	$return['account']=$_SESSION['account'];
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['groupid']=$fenleiid ? $fenleiid : 0;
	$return['fenleiname']=$fenlei[$fenleiid]['title'];
	$return['groups']=$fenlei?$fenlei:array();
	$return['list']=$list?$list:array();
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	set_html('title',$return['fenleiname'].'-'.$_SESSION['account']['title']);
	exi($return);
}
if($_RQ['op']=='detail'){
	//刷新阅读次数
	pdo_update('core_account_article',array('re_readnum +='=>1),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	//获取文章数据
	$article_id=intval($_RQ['id']);
	$return['item']=pdo_get('core_account_article',array('id'=>$article_id,'uniacid'=>$_SESSION['uniacid']),array('id','title','readnum','likenum','author','adtype','adtitle','admore','adurl','advs','date','account','re_readnum','oldurl','yingxiao','content','fenlei','createtime'));
	if(!$return['item']){
		exi('指定文章不存在！','error','list');
	}
	$return['item']['readnum'] = $return['item']['readnum']+$return['item']['re_readnum'];
	unset($return['item']['re_readnum']);
	//点赞数=虚拟点赞数+真实点赞数
	$return['item']['likenum']+=pdo_count('core_account_article_like',array('article_id'=>$article_id,'like'=>1));
	//unset($return['item']['re_likenum']);
	$return['item']['advs']=json_decode($return['item']['advs'],true);
	$return['item']['createtime']=date('20y-m-d',$return['item']['createtime']);
	$return['fenlei']=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['item']['fenleiurl']=custom_url('jy_weishop/news.list//id='.$return['item']['fenlei'],true);
	$return['item']['fenlei']=$return['fenlei'][$return['item']['fenlei']]['title'];
	$advs=array();
	if($return['item']['yingxiao']==1){
		$advs[]=$return['item']['advs'][0];
	}elseif($return['item']['yingxiao']==2){
		$num=count($return['item']['advs']);
		$return['index']=mt_rand(0,$num-1);
		$advs[]=$return['item']['advs'][$return['index']];
	}else{
		$advs=$return['item']['advs'];
	}
	$return['item']['advs']=$advs;
	//$return['item']['content'] = str_replace('&nbsp;','',$return['item']['content']);
	$content=strip_tags($return['item']['content']);
	$return['item']['desc'] = $return['item']['desc'] ? $return['item']['desc'] : cfc('tools')->substr($content,0,36);
	$sharelink='jy_weishop/news.list/detail/fromuser='.$_SESSION['uid'].'&id='.$return['item']['id'];
	$return['sharedata']=array(
			'title'=>$return['item']['title'] . '-' . $_SESSION['account']['title'],
			'desc'=>$return['item']['desc'],
			'link'=>custom_url($sharelink,true),
			'imgUrl'=>tomedia($return['item']['pic_url'])
	);
	$return['account']=$_SESSION['account'];
	//检查当前用户是否点赞
	if($_SESSION['uid']){
		$return['like']=pdo_get('core_account_article_like',array('article_id'=>$article_id,'mid'=>$_SESSION['uid']),array('id','like'));
		
	}
	//获取站点内容页模板
	if($_RQ['siteid'] && $_SESSION['os']=='windows'){
		$siteid=intval($_RQ['siteid']);
		$return['tpl']= cfc('template')->get_html($siteid,'desc');
		$return['htmls']=<<<TPL
<div ng-include="tomedia(pagedata.tpl.head_html)"></div>
<div ng-include="tomedia(pagedata.tpl.type)"></div>
<div ng-include="tomedia(pagedata.tpl.foot_html)"></div>
TPL;
	}
	set_html('title',$return['item']['title']);
	exi($return);
}
if($_RQ['op']=='like'){
	$post=get_postdata();
	if(!$_SESSION['uid']){
		exi('success');
	}
	$id=intval($post['id']);
	$like=intval($post['like']);
	$data=array(
		'like'=>$post['like']?1:0,
		'updatetime'=>TIMESTAMP,
	);
	if($id){
		pdo_update('core_account_article_like',$data,array('id'=>$id,'mid'=>$_SESSION['uid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['article_id']=intval($post['article_id']);
		$data['mid']=$_SESSION['uid'];
		pdo_insert('core_account_article_like',$data);
	}
	exi('success');
}
